var addressVue = new Vue({
	el: "body",
	data: {
		defaultAddress: [],
		normalAddress: []
	},
	created: function() {
		init();
		_this = this;
		verifyLogin(_this)
	},
	methods: {
		resetDefault: function(a) {
			var newDefault = _this.normalAddress[a]
			var oldDefault = _this.defaultAddress[0]
			var oldrid = oldDefault.id
			var newrid = newDefault.id
			var todo = AV.Object.createWithoutData('Address', oldrid);
			// 修改属性
			todo.set('isDefault', 'none');
			// 保存到云端
			todo.save();
			var todo = AV.Object.createWithoutData('Address', newrid);
			// 修改属性
			todo.set('isDefault', 'default');
			// 保存到云端
			todo.save();
			_this.normalAddress.splice(a, 1)
			_this.normalAddress.push(oldDefault)
			_this.defaultAddress = []
			_this.defaultAddress.push(newDefault)
		}
	}
})

function loadDefaultAddress(t) {
	var username = currentUser.attributes.username
	var user = new AV.Query('Address');
	user.equalTo("user", username);
	var isDefault = new AV.Query('Address');
	isDefault.equalTo("isDefault", "default");
	var query = AV.Query.and(user, isDefault);
	query.find().then(function(products) {
		t.defaultAddress = products;
	}).catch(function(error) {
		console.log(JSON.stringify(error));
	});
}

function loadNormolAddress(t) {
	var username = currentUser.attributes.username
	var user = new AV.Query('Address');
	user.equalTo("user", username);
	var isDefault = new AV.Query('Address');
	isDefault.equalTo("isDefault", "none");
	var query = AV.Query.and(user, isDefault);
	query.find().then(function(products) {
		t.normalAddress = products;
	}).catch(function(error) {
		console.log(JSON.stringify(error));
	});
}

function verifyLogin(a) {
	currentUser = AV.User.current();
	if(currentUser) {
		loadDefaultAddress(a)
		loadNormolAddress(a)
	} else {
		location.href = "../login/login.html?RetUrl=" + getPageUrl()
	}
}